﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PHTShop.admin
{
    public partial class productList : System.Web.UI.Page
    {
        private DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Product list";
            if (!IsPostBack)
            {
                loadLanguage();
                dataForListView(Convert.ToInt32(DropDownList1.SelectedValue));
                int check = dc.Products.ToList().Count;
                if (check==0)
                {
                    DataPager1.Visible = false;
                }
              
            }
        }

        private void loadLanguage(){
            DropDownList1.DataSource = dc.Languages;
            DropDownList1.DataValueField = "id";
            DropDownList1.DataTextField = "name";
            DropDownList1.DataBind();
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int language = Convert.ToInt32(DropDownList1.SelectedValue);
            dataForListView(language);
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            LinkButton lb = sender as LinkButton;
            HiddenField hf = lb.FindControl("HiddenField1") as HiddenField;
            int productId = Convert.ToInt32(hf.Value);
            Product p = dc.Products.SingleOrDefault(z => z.id == productId);
            p.status = false;
            dc.SubmitChanges();
            dataForListView(Convert.ToInt32(DropDownList1.SelectedValue));

        }

        private void dataForListView(int languageId)
        {
           
           if (!string.IsNullOrEmpty(Request.QueryString["partnerID"]))
            {
                int idPartenr = Convert.ToInt32(Request.QueryString["partnerID"]);
                var list = from cl in dc.CategoriesLanguages
                           join c in dc.Categories on cl.categoriesId equals c.id
                           join p in dc.Products on c.id equals p.categoriesId
                           join pl in dc.ProductsLanguages on p.id equals pl.productsId
                           where cl.languageId == languageId && pl.languageId == languageId && p.PartnersId==idPartenr
                           select new { productname = pl.name, categoryname = cl.name, price = p.price, productId = p.id, quantity = p.quantity, p, status = p.status,catID=c.id };

                ListView1.DataSource = list;
                ListView1.DataBind();
            }
           else if (!string.IsNullOrEmpty(Request.QueryString["categoryID"]))
           {
               int idCat = Convert.ToInt32(Request.QueryString["categoryID"]);
               var list = from cl in dc.CategoriesLanguages
                          join c in dc.Categories on cl.categoriesId equals c.id
                          join p in dc.Products on c.id equals p.categoriesId
                          join pl in dc.ProductsLanguages on p.id equals pl.productsId
                          where cl.languageId == languageId && pl.languageId == languageId && c.id == idCat
                          select new { productname = pl.name, categoryname = cl.name, price = p.price, productId = p.id, quantity = p.quantity, p, status = p.status, catID = c.id };

               ListView1.DataSource = list;
               ListView1.DataBind();
           }
           else {
               var list = from cl in dc.CategoriesLanguages
                          join c in dc.Categories on cl.categoriesId equals c.id
                          join p in dc.Products on c.id equals p.categoriesId
                          join pl in dc.ProductsLanguages on p.id equals pl.productsId
                          where cl.languageId == languageId && pl.languageId == languageId
                          select new { productname = pl.name, categoryname = cl.name, price = p.price, productId = p.id, quantity = p.quantity, p, status = p.status, catID = c.id };

               ListView1.DataSource = list;
               ListView1.DataBind();
           }
         

        }

        protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            this.DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

            int languageId = Convert.ToInt32(DropDownList1.SelectedValue);

            if (!string.IsNullOrEmpty(Request.QueryString["partnerID"]))
            {
                int idPartenr = Convert.ToInt32(Request.QueryString["partnerID"]);
                var list = from cl in dc.CategoriesLanguages
                           join c in dc.Categories on cl.categoriesId equals c.id
                           join p in dc.Products on c.id equals p.categoriesId
                           join pl in dc.ProductsLanguages on p.id equals pl.productsId
                           where cl.languageId == languageId && pl.languageId == languageId && p.PartnersId == idPartenr
                           select new { productname = pl.name, categoryname = cl.name, price = p.price, productId = p.id, quantity = p.quantity, p, status = p.status, catID = c.id };

                ListView1.DataSource = list;
                ListView1.DataBind();
            }
            else if (!string.IsNullOrEmpty(Request.QueryString["categoryID"]))
            {
                int idCat = Convert.ToInt32(Request.QueryString["partnerID"]);
                var list = from cl in dc.CategoriesLanguages
                           join c in dc.Categories on cl.categoriesId equals c.id
                           join p in dc.Products on c.id equals p.categoriesId
                           join pl in dc.ProductsLanguages on p.id equals pl.productsId
                           where cl.languageId == languageId && pl.languageId == languageId && c.id == idCat
                           select new { productname = pl.name, categoryname = cl.name, price = p.price, productId = p.id, quantity = p.quantity, p, status = p.status, catID = c.id };

                ListView1.DataSource = list;
                ListView1.DataBind();
            }
            else
            {
                var list = from cl in dc.CategoriesLanguages
                           join c in dc.Categories on cl.categoriesId equals c.id
                           join p in dc.Products on c.id equals p.categoriesId
                           join pl in dc.ProductsLanguages on p.id equals pl.productsId
                           where cl.languageId == languageId && pl.languageId == languageId
                           select new { productname = pl.name, categoryname = cl.name, price = p.price, productId = p.id, quantity = p.quantity, p, status = p.status, catID = c.id };

                ListView1.DataSource = list;
                ListView1.DataBind();
            }
        }

    }
}